package com.hjk.waibao.mj.bean;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * api信息标准返回
 *
 * @author 洪嘉凯
 */
@Data
@AllArgsConstructor
public class ApiResult {
    public static final Integer SUCCESS_CODE = 200;
    public static final Integer FAIL_CODE = 500;
    /**
     * 授权码不存在错误
     */
    public static final Integer AUTHORIZE_CODE_NOT_FOUND_CODE = 501;
    /**
     * 授权码过期错误
     */
    public static final Integer AUTHORIZE_CODE_EXPIRE_CODE = 502;
    /**
     * 授权码已登录错误
     */
    public static final Integer AUTHORIZE_CODE_LOGGED_CODE = 503;
    /**
     * 授权码已登录错误
     */
    public static final Integer AUTHORIZE_CODE_MACHINE_LOGOUT_CODE = 504;

    Integer code;
    String mess;
    Object data;

    public static ApiResult create(Integer code, String mess, Object data) {
        return new ApiResult(code, mess, data);
    }

    public static String createJson(Integer code, String mess, Object data) {
        return new ApiResult(code, mess, data).toJsonString();
    }

    public static ApiResult createSuccess(String mess, Object data) {
        return create(SUCCESS_CODE, mess, data);
    }

    public static ApiResult createSuccessWithJsonData(String mess, String data) {
        return create(SUCCESS_CODE, mess, JSONObject.parse(data));
    }

    public static String createSuccessWithJsonDataJson(String mess, String data) {
        return createSuccessWithJsonData(mess, data).toJsonString();
    }

    public static String createSuccessJson(String mess, Object data) {
        return create(SUCCESS_CODE, mess, data).toJsonString();
    }

    public static ApiResult createSuccess(Object data) {
        return create(SUCCESS_CODE, "成功", data);
    }

    public static String createSuccessJson(Object data) {
        return create(SUCCESS_CODE, "成功", data).toJsonString();
    }

    public static ApiResult createFail(String mess) {
        return create(FAIL_CODE, mess, "");
    }

    public static String createFailJson(String mess) {
        return create(FAIL_CODE, mess, "").toJsonString();
    }

    public String toJsonString() {
        JSONObject json = new JSONObject();
        json.put("code", code);
        json.put("mess", mess);
        json.put("data", data);
        return json.toString();
    }
}
